@using System.Reflection
@using StudentVideo.Client.Common.Constants
@using StudentVideo.Shared.Enums
@using StudentVideo.Shared.Components.CuForm;
@using StudentVideo.Client.Components.CuIcon;
@using static AntDesign.IconType

@typeparam TFormModel;

<div id="cuForm" style="height: @(ONE_LINE_HEIGHT)px;">
   <div @ref="formBox" class="form_box @(_isExpand ? "form_box_show" : "")" style="height: @(_boxHeight)px;">
      @if (_showExpandButton)
      {
         <Popover Content="@(_isExpand ? "点击收缩" : "点击展开")">
            <div class="expand" onclick="@ChangeExpandStatus">
               @if (_isExpand)
               {
                  <CuIcon IconType="@CuIconEnum.Reduce" Square="24" />
               }
               else
               {
                  <CuIcon IconType="@CuIconEnum.Extend" Square="24" />
               }
            </div>
         </Popover>
      }

      <Form class="ant_formModule" @ref="EleForm" TModel="TFormModel" Model="@FormModel" Layout="FormLayout.Inline">
         @foreach (PropertyInfo prop in _properties)
         {
            CuFormMateAttribute? mate = prop.GetCustomAttribute<CuFormMateAttribute>();
            if (mate == null)
               continue;

            Type propType = prop.GetType();

            @switch (mate.FormType)
            {
               // 文本输入
               case CuFormTypeEnums.Text:
                  {
                     var val = GetPropertyValue<string>(FormModel, prop);

                     <FormItem class="form_item form_item_input">
                        <Input TValue="string" @Value="@(val)" ValueChanged="@(e => SetPropertyValue<string>(FormModel, prop, e))" Placeholder="@mate.Help" />
                     </FormItem>
                     break;
                  }
               // 选择框
               case CuFormTypeEnums.Select:
                  {
                     var options = SelectOptionsDic[prop.Name];

                     var val = GetPropertyValue<string>(FormModel, prop);

                     <FormItem class="form_item form_item_select">
                        <Select Mode="@(propType.IsArray? SelectMode.Multiple: SelectMode.Default)" TItemValue="string" TItem="string" EnableSearch AllowClear Value="@val" ValuesChanged="@(e => SetPropertyValue<object>(FormModel, prop, e))" Placeholder="@mate.Help">

                           <SelectOptions>
                              @foreach (var option in options)
                              {
                                 <SelectOption TItemValue="string" TItem="string" Value=@option.Value.Val Label=@option.Value.Label />
                              }
                           </SelectOptions>
                        </Select>
                     </FormItem>

                     break;
                  }
               case CuFormTypeEnums.DateRange:
                  {
                     <FormItem class="form_item form_item_datetime">
                        <RangePicker TValue="DateOnly?[]" OnChange="@(e => SetPropertyValue<DateOnly?[]>(FormModel, prop, e.Dates))" Placeholder="GetHelps(mate, prop)" />
                     </FormItem>
                     break;
                  }
               case CuFormTypeEnums.DateTimeRange:
                  {
                     <FormItem class="form_item form_item_datetime">
                        <RangePicker TValue="DateTime?[]" ShowTime="@true" OnChange="@(e => SetPropertyValue<DateTime?[]>(FormModel, prop, e.Dates))" Placeholder="GetHelps(mate, prop)" />
                     </FormItem>
                     break;
                  }
               default: throw new ArgumentOutOfRangeException();
            }
         }
      </Form>
      <div class="cuForm_buttons @ButtonsClassName">
         @if (ShowSearchButton)
         {
            <Button Type="ButtonType.Primary" OnClick="SearchHandler">查询</Button>
         }
         @CustomsButtons
      </div>
   </div>
</div>
